/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyDatabase;

import java.util.ArrayList;

/**
 *
 * @author Welcome
 */
public class DatabaseHandler {
    
    //<editor-fold defaultstate="collapsed" desc="1/Cac thuoc tinh co ban">
    private String databaseName;
    private ArrayList<MyTable> tables;
    private XMLHandler xmlHandle;
    private String pathFile;
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="2/Cac phuong thuc khoi tao">
    public DatabaseHandler(String _pathFile) {
        this.pathFile=_pathFile;
        tables=new ArrayList<MyTable>();
        xmlHandle = new XMLHandler(pathFile);
        
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="3/Cac phuong thuc Getter/Setter">
    public String getDatabaseName() {
        return databaseName;
    }
    
    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }
    
    public void setPathFile(String pathFile) {
        this.pathFile = pathFile;
    }
    
    public void setTables(ArrayList<MyTable> tables) {
        this.tables = tables;
    }
    
    public void setXmlHandle(XMLHandler xmlHandle) {
        this.xmlHandle = xmlHandle;
    }
    
    
    public String getPathFile() {
        return pathFile;
    }
    
    public XMLHandler getXmlHandle() {
        return xmlHandle;
    }
    public ArrayList<MyTable> getTables() {
        return tables;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="4/Cac phuong thuc xu ly nghiep vu">
    public void ReadDatabase()
    {
        databaseName = findDatabaseName();
        LoadDataIntoTables();
    }
    
    public void WriteDatabase(String pathFile)
    {
        xmlHandle.writeDatabaseToXMLfile(pathFile,this);
    }
    
    public void showTest()
    {
        //he he, you know what I am doing here :D
        ReadDatabase();
        System.out.println("Database name "+databaseName);
        for (int i = 0; i < tables.size(); i++)
        {
            tables.get(i).showTable();
        }
    }
    
    private String findDatabaseName() {
        String tempString="";
        tempString = xmlHandle.getDatabaseName();
        return tempString;
    }
    
    private void LoadDataIntoTables() {
        xmlHandle.SetDataIntoTable(tables);
    }
    
    public String getNumberTablesByString() {
        String result = String.valueOf(tables.size());
        return result;
    }
    //</editor-fold>
    
}
